1
'****************************** Module Header ******************************\
2 ' Module Name: ShutdownBehavior.vb
3 ' Project: VBWPFMVVMPractice
4 ' Copyright (c) Microsoft Corporation.
6 ' The ShutdownBehavior class is an attached behavior that is used to shut down the application.
8 ' This source is subject to the Microsoft Public License.
9 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 ' All other rights reserved.
12 ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
13 ' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
14 ' WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 '***************************************************************************/
17 Public Class ShutdownBehavior
18 Inherits DependencyObject
20 Public Shared
Function GetForceShutdown(ByVal obj
As DependencyObject
) As Nullable(Of
Boolean)
21 Return DirectCast(obj
.GetValue(ShutdownBehavior
.ForceShutdownProperty
), Nullable(Of
Boolean))
24 Public Shared
Sub SetForceShutdown(ByVal obj
As DependencyObject
, ByVal value
As Nullable(Of
Boolean))
25 obj
.SetValue(ShutdownBehavior
.ForceShutdownProperty
, value
)
28 ' Using a DependencyProperty as the backing store for ForceShutdown. This enables
29 ' animation, styling, binding, etc...
30 Public Shared ReadOnly ForceShutdownProperty
As DependencyProperty
=
31 DependencyProperty
.RegisterAttached(
33 GetType(Nullable(Of
Boolean)),
34 GetType(ShutdownBehavior
),
35 New UIPropertyMetadata(
37 Function(sender
As DependencyObject
, e
As DependencyPropertyChangedEventArgs
)
38 ShutdownBehavior
.OnPropertyChangedCallBack(sender
, e
)
42 ''' MenuItem's Click event handler
44 Private Shared
Sub mnu_Click(ByVal sender
As Object, ByVal e
As RoutedEventArgs
)
46 ' If the ForceShutdown attached proerty value set on the MenuItem is true, exit the
47 ' application immediately.
48 If ShutdownBehavior
.GetForceShutdown(TryCast(sender
, DependencyObject
)).Value
Then
49 Application
.Current
.Shutdown()
51 ' If the ForceShutdown attached proerty value set on the MenuItem is false, show a
52 ' messagebox before exiting the application.
53 ElseIf (Not ShutdownBehavior
.GetForceShutdown(TryCast(sender
, DependencyObject
)).Value AndAlso
54 (MessageBox
.Show("Are you sure to exit the application?",
55 "Exit", MessageBoxButton
.OKCancel
) = MessageBoxResult
.OK
)) Then
56 Application
.Current
.Shutdown()
61 ''' The ForceShutdown property's PropertyChangedCallback method.
62 ''' In this method, get the MenuItem the attached property is set on and subscribe to the
63 ''' Click event of the MenuItem.
65 Private Shared
Sub OnPropertyChangedCallBack(ByVal sender
As DependencyObject
,
66 ByVal e
As DependencyPropertyChangedEventArgs
)
67 Dim mnu
As MenuItem
= TryCast(sender
, MenuItem
)
69 ' This ensures the Click event on the MenuItem is subscribed to only once.
70 If (Not mnu Is
Nothing) Then
71 RemoveHandler mnu.Click, New RoutedEventHandler(AddressOf ShutdownBehavior.mnu_Click)
72 AddHandler mnu
.Click
, New RoutedEventHandler(AddressOf ShutdownBehavior
.mnu_Click
)